package com.yanqu.road.server.manager.activity.selectlottery;

import com.yanqu.road.dao.impl.activity.selectlottery.SelectLotteryNoticeDataDaoImpl;
import com.yanqu.road.entity.activity.selectlottery.config.SelectLotteryConfig;
import com.yanqu.road.entity.activity.selectlottery.data.SelectLotteryNoticeData;
import com.yanqu.road.pb.activity.SelectLotteryProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.selectlottery.pb.CrossSelectLotteryPb;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.ArrayList;
import java.util.List;

public class CrossSelectLotteryActivity {

    /**
     * 活动ID
     */
    private int activityId;

    /**
     * 配置
     */
    private SelectLotteryConfig config;

    /**
     * 公告数据
     */
    private List<SelectLotteryNoticeData> noticeList;

    public CrossSelectLotteryActivity(SelectLotteryConfig config) {
        this.config = config;
        this.activityId = config.getActivityInfo().getActivityId();
        //加载数据
        this.reloadData();
    }

    /**
     * 加载数据
     */
    private void reloadData() {
        SelectLotteryNoticeDataDaoImpl dao = new SelectLotteryNoticeDataDaoImpl();
        //加载数据
        this.noticeList = dao.getSelectLotteryNoticeData(this.activityId, this.config.SELECT_LOTTERY_EXTRACT_NOTICE_NUM);
    }

    /**
     * 存储
     */
    public void save() {
        List<SelectLotteryNoticeData> saveList;
        synchronized (this.noticeList) {
            saveList = new ArrayList<>(this.noticeList);
            //公告大于最大条数，移除超出的条数，较少内存压力
            int maxNum = this.config.SELECT_LOTTERY_EXTRACT_NOTICE_NUM;
            if (this.noticeList.size() > maxNum) {
                int removeCount = noticeList.size() - maxNum;
                this.noticeList.subList(0, removeCount).clear(); // 移除最早添加的元素
            }
        }
        //存储数据
        SelectLotteryNoticeDataDaoImpl dao = new SelectLotteryNoticeDataDaoImpl();
        for (SelectLotteryNoticeData noticeData : saveList) {
            if (noticeData.isInsertOption()) {
                dao.add(noticeData);
            }
        }
    }

    /**
     * 获取活动ID
     * @return
     */
    public int getActivityId() {
        return activityId;
    }

    /**
     * 获取配置
     * @return
     */
    public SelectLotteryConfig getConfig() {
        return config;
    }

    /**
     * 刷新配置
     * @param config
     */
    public void refreshConfig(SelectLotteryConfig config) {
        this.config = config;
    }

    /**
     * 获取公告LIST
     * @return
     */
    public List<SelectLotteryNoticeData> getNoticeList() {
        return this.noticeList;
    }

    /**
     * 添加公告
     * @param userId
     * @param serverId
     * @param rewardId
     * @param nickName
     * @param time
     */
    public void addNotice(long userId, int serverId, int rewardId, String nickName, long time) {
        SelectLotteryNoticeData noticeData = new SelectLotteryNoticeData(
                this.activityId,
                userId,
                serverId,
                rewardId,
                nickName,
                time
        );
        synchronized (this.noticeList) {
            this.noticeList.add(noticeData);
        }
        //同步区服玩家
        for (long activityServerId : this.getConfig().getActivityInfo().getServerIdList()) {
            SelectLotteryProto.SelectLotteryNoticeData.Builder noticeBuilder = CrossSelectLotteryPb.buildSelectLotteryNoticeData(noticeData);
            SelectLotteryProto.SelectLotterySyncNoticeFromCross.Builder msg = SelectLotteryProto.SelectLotterySyncNoticeFromCross.newBuilder();
            msg.setNoticeData(noticeBuilder);
            MessageHelper.sendPacket(activityServerId, 0, YanQuMessageUtils.buildMessage(Protocol.S_SELECT_LOTTERY_SYNC_NOTICE_FROM_CROSS, msg));
        }
    }
}
